import { RequestGoPay } from '@/api/cart'
import { useGoPayStore } from '@/stores/goPay'
import type { IGoPayList } from '@/types/types'
import { Toast } from 'vant'
import { ref, type Ref, onMounted, computed } from 'vue'
import { usePayOder } from './payOder'
import { useRouter } from 'vue-router'
import { useOnAddStore } from '@/stores/onAdd'
import { userStore } from '@/stores/user'


export const useGoPay = () => {
    const goPayStore = useGoPayStore()
    const onAddStore = useOnAddStore()
    const userCount = userStore()
    const goPayList: Ref<Array<IGoPayList>> = ref([])
    const router = useRouter()

    const getGoPay = async (
        userId: number,
        productIds: string,
        phone: string,
        concats: string,
        address: string
    ) => {
        const data = await RequestGoPay(
            userId,
            productIds,
            phone,
            concats,
            address
        )
        const { resultCode, resultInfo } = data
        if (resultCode === 1) {
            // console.log(resultInfo)
            goPayList.value = resultInfo.orderNum
            console.log(goPayList.value)

            goPayStore.saveGoPayList(goPayList.value)
        }
    }

    const oderNum = computed(() => {
        return goPayList.value
    })

    const bindPaySccess = () => {
        Toast.success('支付成功')
        router.replace({ path: '/home' })
    }

    
    onMounted(() => {
        let id = userCount.getCount.uid
        let orderid = onAddStore.getOrderid
        
        getGoPay(id, orderid, onAddStore.addRess.tel, onAddStore.addRess.username, onAddStore.addRess.address)
    })
    return { goPayStore, goPayList, getGoPay, oderNum, bindPaySccess }
}
